﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DispatchAssist.Service;
using CarolLib;
using DispatchAssist.Model;
using DispatchAssist.BusinessLogical.Model;

namespace DispatchAssist.AppHost
{
    public partial class frmViewAlertActLogDetail : Form
    {
        AuthService service = new AuthService();
        ClientMessageInfo clientinfo = new ClientMessageInfo();
        QueryActLog qalog = new QueryActLog();
        public bool isViewDialog = true;

        public frmViewAlertActLogDetail()
        {
            InitializeComponent();
        }

        public frmViewAlertActLogDetail(QueryActLog qal)
            : this()
        {
            qalog = qal;
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmViewAlertActLogDetail_Load(object sender, EventArgs e)
        {
            service.DASoapHeaderValue = AuthContext.Current.GetSoapHeader();

            string jsonstr = service.GetAllAlertInfoData(qalog.AlertID, qalog.AlertAction);

            // ClientMessageInfo messinfo = new ClientMessageInfo();
            if (!string.IsNullOrEmpty(jsonstr))
            {
                try
                {
                    clientinfo = JsonSerializeHelper.DataContractJsonDeserializeByJNet<ClientMessageInfo>(jsonstr);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }

            }

            BindDetailInfo();
        }

        private void BindDetailInfo()
        {
            if (qalog == null) return;
            object obj = null;
            if (qalog.AlertAction == (int)Enums.AlertActionFlag.ConfirmSMS || qalog.AlertAction == (int)Enums.AlertActionFlag.ManualSendSMSAlert)
            {
                obj = GetAlertInfoByAlertID(1, qalog.AlertID);
                if (obj == null) { isViewDialog = false; return; }

                //AlertAllInfo info = (AlertAllInfo)obj;
                BindAlertDetailControl(1, obj);
            }
            else
            {
                obj = GetAlertInfoByAlertID(0, qalog.AlertID);
                if (obj == null) { isViewDialog = false; return; }
                //SMSAlertAllInfo info = (SMSAlertAllInfo)obj;
                BindAlertDetailControl(0, obj);

            }
            //txtFlightNo.Text = alertinfo.Carrier + alertinfo.FlightNo;
            //txtTailNo.Text = alertinfo.TailNo;
            //txtAirPort.Text = alertinfo.AirportFullName;
            //txtAlertLevel.Text = alertinfo.WarnLevelName;
            //txtAlertTime.Text = alertinfo.WarnTime.ToString();
            ////txtAlertAction.Text = alertinfo.AlertActionFlag.ToString();
            //richtxtACARSContent.Text = alertinfo.MessageBody;
        }


        private void BindAlertDetailControl(int flag, object info)
        {

            switch (flag)
            {
                case 0:
                    //todo:重构过，删掉了之前的groupbox
                    //grpDetailInfo.Visible = true;
                    //grpDetailInfo.Dock = DockStyle.Fill;
                    //grpSMSDetailInfo.Visible = false;

                    NormalAlertInfo alinfo = (NormalAlertInfo)info;
                    palSms.Visible = false;
                    palSms.Dock = DockStyle.None;

                    palNotSms.Visible = true;
                    palNotSms.Dock = DockStyle.Fill;

                    txtFlightNo.Text = alinfo.Carrier + alinfo.FlightNo;
                    txtTailNo.Text = alinfo.TailNo;
                    txtAlertLevel.Text = alinfo.WarnLevelName;
                    txtAlertTime.Text = alinfo.WarnTime.ToString();
                    txtAirPort.Text = alinfo.AirportFullName;
                    rtbMessageContent.Text = alinfo.MessageBody;
                    rtbMessageContent.BackColor = palNotSms.BackColor;
                    break;

                case 1:
                    SMSAlertAllInfo smsalinfo = (SMSAlertAllInfo)info;
                    palNotSms.Dock = DockStyle.None;
                    palNotSms.Visible = false;

                    palSms.Visible = true;
                    palSms.Dock = DockStyle.Fill;

                    txtSMSAlertLevel.Text = smsalinfo.WarnLevelName;
                    txtSMSAlertTime.Text = smsalinfo.WarnTime.ToString();
                    txtSMSAirPort.Text = smsalinfo.AirportFullName;
                    txtGroupCode.Text = smsalinfo.GroupCode;
                    rtbSMSContent.Text = smsalinfo.MessageBody;
                    rtbSMSContent.BackColor = palSms.BackColor;
                    break;
                default:
                    break;
            }

            btnClose.Focus();

        }
        private object GetAlertInfoByAlertID(int flag, Guid alertID)
        {
            object obj = null;
            switch (flag)
            {
                case 0:
                    obj = clientinfo.NormalAlertList.Find(s => s.AlertID == alertID);
                    break;
                case 1:
                    obj = clientinfo.SMSAlertList.Find(s => s.SMSAlertID == alertID);
                    break;
                default:

                    break;
            }
            return obj;

        }


    }
}